package com.jams.goodox.activity.serve;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.entity.ServicePauseTime;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.AnimUtils;
import com.jams.goodox.utils.DensityUtil;
import com.jams.goodox.widget.LoadingDialog;

import retrofit2.Call;


/**
 * 服务暂停时间公用类
 * Created by jams_zhen on 2016/10/9.
 */

public class ServeStopTime implements View.OnClickListener {

    private TextView txServiceStopExplain;
    private ImageView imgClose;
    private RelativeLayout relaNoMicro;
    private TextView tx_publicMsg;

    private InStopTime mInStopTime;

    private MaterialDialog hintDialog;
    private String msg = "自2016-09-01至2016-09-07暂停服务，在此期间内您所下的单，我们将在2016-09- 08开始接单处理，给您带来不便，敬请谅解！";//提示信息

    private Context mContext;


    private OnNextClickIn onNextClickIn;

    private LoadingDialog loadingDialog;
    private LinearLayout.LayoutParams layoutParams;

    public ServeStopTime(Context context) {
        this.mContext = context;
    }

    public ServeStopTime(Context context, InStopTime inStopTime) {
        this.mContext = context;
        this.mInStopTime = inStopTime;
    }

    /**
     * 控件以及弹框初始化
     */
    public void init(String id) {
        txServiceStopExplain = (TextView) ((Activity) mContext).findViewById(R.id.tx_serviceStop_explain);
        imgClose = (ImageView) ((Activity) mContext).findViewById(R.id.img_close);
        relaNoMicro = (RelativeLayout) ((Activity) mContext).findViewById(R.id.rela_noMicro);
        tx_publicMsg = (TextView) ((Activity) mContext).findViewById(R.id.tx_publicMsg);

        hintDialog = new MaterialDialog.Builder(mContext).title("公告").content(msg).negativeText("返回").onNegative(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                hintDialog.cancel();
                if (onNextClickIn != null)
                    onNextClickIn.onClick(false);
                else
                    ((Activity) mContext).finish();
            }
        }).positiveText("继续").onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (onNextClickIn != null)
                    onNextClickIn.onClick(true);
                else
                    hintDialog.cancel();
                getFouces();
            }
        }).build();

        txServiceStopExplain.setOnClickListener(this);
        imgClose.setOnClickListener(this);
        relaNoMicro.setOnClickListener(this);

        layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(mContext,35));
        getDate(id);
    }

    /**
     * 需要单独处理弹框和滚动
     *
     * @param onNextClickIn
     */
    public void init(OnNextClickIn onNextClickIn, String id) {
        this.onNextClickIn = onNextClickIn;
        init(id);
    }

    /**
     * 点击弹出弹框
     */
    public void showDialog() {
        if (hintDialog != null)
            hintDialog.show();
    }

    /**
     * 请求接口
     */
    private void getDate(String id) {
        if(!(mContext instanceof BaseActivity)|| TextUtils.isEmpty(id))return;
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.createDialog(mContext);
        }
        loadingDialog.show();
        ((BaseActivity)mContext).enqueue(((BaseActivity)mContext).createCallService(ServiceService.class).getServiceStopTime(id),new RetrofitCallbackListener<ServicePauseTime>(){
            @Override
            public void onSuccess(Call<ServicePauseTime> call, ServicePauseTime result) {
                loadingDialog.cancel();
                if (result.getData() == null)
                    return;
                setDate(result.getData());
            }

            @Override
            public void onError(String msg) {
                loadingDialog.cancel();
            }
        });
    }

    private void setDate(ServicePauseTime.DataBean dataBean) {
        if (mInStopTime != null)
            mInStopTime.setViewVisiable();

        if (dataBean == null)
            return;

        msg = dataBean.getServiceMsg();
        hintDialog.setContent(msg);
        txServiceStopExplain.setText(msg);
        tx_publicMsg.setText(msg);

        switch (dataBean.getServiceTimeType()) {
            case 0:
                txServiceStopExplain.setVisibility(View.GONE);
                break;
            case 1:
                txServiceStopExplain.setVisibility(View.VISIBLE);
                txServiceStopExplain.setLayoutParams(layoutParams);
                getFouces();
                break;
            case 2:// TODO: 2017/7/18 在有弹框时，跑马灯效果无 
                //滚动需要获取焦点
                txServiceStopExplain.setVisibility(View.VISIBLE);
                txServiceStopExplain.setLayoutParams(layoutParams);
                getFouces();
                if (onNextClickIn == null)
                    hintDialog.show();
                break;
            default:
                txServiceStopExplain.setVisibility(View.GONE);
                hintDialog.cancel();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tx_serviceStop_explain:
                relaNoMicro.setVisibility(View.VISIBLE);
                AnimUtils.showAnim(relaNoMicro,500);
                break;
            case R.id.img_close:
                relaNoMicro.setVisibility(View.GONE);
                AnimUtils.misAnim(relaNoMicro,500);
                break;
            case R.id.rela_noMicro:
                relaNoMicro.setVisibility(View.GONE);
                AnimUtils.misAnim(relaNoMicro,500);
                break;
        }
    }

    public interface OnNextClickIn {
        public void onClick(boolean isNext);
    }

    private void getFouces() {
        //滚动需要获取焦点
        txServiceStopExplain.requestFocus();
    }

    /**
     * 处理view显示，主要是滚动条由gone->visiable不会自动滚动
     */
    public interface InStopTime {
        public void setViewVisiable();
    }
}
